﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class ManipulateCellFormulas
	{
		[Test]
		public void InsertRows()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.InsertRows(1, 1);
				Assert.AreEqual("SUM(A1:A11)+SUM($C1:$C11)+SUM(E$1:E$11)+SUM($G$1:$G$11)+I1+$I4+I$6+$I$8", part.Worksheet.GetCellFormula(new GridReference(12, 0)), "Initial cell formula");
			}
		}

		[Test]
		public void InsertColumns()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.InsertColumns(1, 1);
				Assert.AreEqual("SUM(A1:A10)+SUM($D1:$D10)+SUM(F$1:F$10)+SUM($H$1:$H$10)+J1+$J3+J$5+$J$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
			}
		}

		[Test]
		public void DeleteRowsNonBreaking()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.DeleteRows(1, 1);
				Assert.AreEqual("SUM(A1:A9)+SUM($C1:$C9)+SUM(E$1:E$9)+SUM($G$1:$G$9)+I1+$I2+I$4+$I$6", part.Worksheet.GetCellFormula(new GridReference(10, 0)), "Initial cell formula");
			}
		}

		[Test]
		public void DeleteColumnsNonBreaking()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.DeleteColumns(1, 1);
				Assert.AreEqual("SUM(A1:A10)+SUM($B1:$B10)+SUM(D$1:D$10)+SUM($F$1:$F$10)+H1+$H3+H$5+$H$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
			}
		}

		[Test]
		public void DeleteRowsBreaking()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.DeleteRows(2, 1);
				Assert.AreEqual("SUM(A1:A9)+SUM($C1:$C9)+SUM(E$1:E$9)+SUM($G$1:$G$9)+I1+#REF!+I$4+$I$6", part.Worksheet.GetCellFormula(new GridReference(10, 0)), "Initial cell formula");
			}
		}

		[Test]
		public void DeleteColumnsBreaking()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.FormulaSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual("SUM(A1:A10)+SUM($C1:$C10)+SUM(E$1:E$10)+SUM($G$1:$G$10)+I1+$I3+I$5+$I$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
				part.Worksheet.DeleteColumns(2, 1);
				Assert.AreEqual("SUM(A1:A10)+SUM(#REF!)+SUM(D$1:D$10)+SUM($F$1:$F$10)+H1+$H3+H$5+$H$7", part.Worksheet.GetCellFormula(new GridReference(11, 0)), "Initial cell formula");
			}
		}
	}
}
